function Rectangle(x, y, w, h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.color = '#cc8811';

    this.lx = 0;
    this.ly = 0;
}

(function() {
    function area(b, x, y) {
        return b.x <= x && (b.x + b.w) >= x && b.y <= y && (b.y + b.h) >= y;
    }

    Rectangle.prototype.draw = function(ctx) {
        ctx.fillStyle = this.color;
        ctx.fillRect(this.x, this.y, this.w, this.h);
    };

    Rectangle.prototype.mousedown = function (x, y) {
        var test = area(this, x, y);
        if (test) {
            this.lx = x;
            this.ly = y;
            this.pressed = true;
        }
        return test;
    };
    
    Rectangle.prototype.mousemove = function(x, y) {
        if (this.pressed) {
            this.x += x - this.lx;
            this.y += y - this.ly;
            this.lx = x;
            this.ly = y;
        }
    };
    
    Rectangle.prototype.mouseup = function (x, y) {
        var test = area(this, x, y);
        if (test) {
            this.pressed = false;
        }
        return test;
    };
})();